<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/17 0017
 * Time: 上午 11:32
 */
class datatableHelper
{
    public $table; //表明
    public $db;//数据库连接

    public function __construct($db, $table)
    {
        $this->db = $db;
        $this->table = $table;
    }

    /**
     * 获取分页
     * @param $link
     * @param $curpage
     * @param string $where
     * @param int $perpage
     * @return mypagerHelper
     */
    public function getPages($link, $curpage, $where = '', $perpage = 20)
    {
        $mypager = new mypagerHelper($this->db, $this->table, $link, $curpage, $where, $perpage);
        return $mypager;
    }

    /**
     * 获得条件列表
     * @param int $start
     * @param string $order
     * @param int $perPage
     * @param string $where
     * @return array
     */
    public function getList($start = 0, $order = '', $perPage = 20, $where = '')
    {
        $sql = "SELECT *  FROM `$this->table`";
        if (!empty($where))
        {
            $sql .= $this->db->getWhere($where);
        }
        if (!empty($order))
        {
            $sql .= $this->db->getOrder($order);
        }
        if ($perPage > 0)
        {
            $sql .= " LIMIT $start,$perPage";
        }
        $list = $this->db->getAll($sql);
        $list = empty($list) ? array() : $list;
        return $list;
    }

    public function getMapList($key, $start = 0, $order = '', $perPage = 20, $where = '')
    {
        $sql = "SELECT *  FROM `$this->table`";
        if (!empty($where))
        {
            $sql .= $this->db->getWhere($where);
        }
        if (!empty($order))
        {
            $sql .= $this->db->getOrder($order);
        }
        if ($perPage > 0)
        {
            $sql .= " LIMIT $start,$perPage";
        }
        $list = $this->db->getAll($sql);
        if (empty($list))
        {
            return array();
        }
        if (!isset($list[0][$key]))
        {
            return $list;
        }
        $newList = array();
        foreach ($list as $v)
        {
            $newList[$v[$key]] = $v;
        }
        unset($list);
        return $newList;
    }

    /**
     * 获取全部符合条件的
     * @param string $order
     * @param string $where
     * @return array
     */
    public function getAll($order = '', $where = '')
    {
        $sql = "SELECT *  FROM `$this->table`";
        if (!empty($where))
        {
            $sql .= $this->db->getWhere($where);
        }
        if (!empty($order))
        {
            $sql .= $this->db->getOrder($order);
        }
        $list = $this->db->getAll($sql);
        $list = empty($list) ? array() : $list;
        return $list;
    }

    /**
     * 获取全部符合条件的
     * @param string $order
     * @param string $where
     * @return array
     */
    public function getMapAll($key, $order = '', $where = '')
    {
        $sql = "SELECT *  FROM `$this->table`";
        if (!empty($where))
        {
            $sql .= $this->db->getWhere($where);
        }
        if (!empty($order))
        {
            $sql .= $this->db->getOrder($order);
        }
        $list = $this->db->getAll($sql);
        if (empty($list))
        {
            return array();
        }
        if (!isset($list[0][$key]))
        {
            return $list;
        }
        $newList = array();
        foreach ($list as $v)
        {
            $newList[$v[$key]] = $v;
        }
        unset($list);
        return $newList;
    }

    /**
     * 删除记录
     * @param $where
     * @return bool
     */
    public function delItem($where)
    {
        if (empty($where))
        {
            return false;
        }
        $this->db->query("DELETE FROM `$this->table` " . $this->db->getWhere($where));
        return true;
    }

    /**
     * 获得一天记录
     * @param $where
     * @return bool
     */
    public function getItem($where, $order = '')
    {
        if (empty($where))
        {
            return false;
        }
        $sql = "SELECT * FROM `$this->table` ";
        if (!empty($where))
        {
            $sql .= $this->db->getWhere($where);
        }
        if (!empty($order))
        {
            $sql .= $this->db->getOrder($order);
        }
        $sql .=' LIMIT 1';
        return $this->db->getOne($sql);
    }

    /**
     * 更新记录
     * @param $arr
     * @param $where
     * @return bool
     */
    public function update($arr, $where)
    {
        $this->db->update($this->table, $arr, $where);
        return $this->db->getAffectedRows() > 0;
    }

    /**
     * 插入一条记录
     * @param $arr
     * @return bool
     */
    public function insert($arr)
    {
        $this->db->insert($this->table, $arr);
        return $this->db->getAffectedRows() > 0;
    }

    public function getFiled($key, $where = '', $order)
    {
        return $this->db->getField($this->table, $key, $where = '', $order);
    }

    public function getMaxId($idKey)
    {
        $result = $this->db->getOne("SELECT MAX($idKey) as `maxId` FROM `$this->table`");
        if (!empty($result))
        {
            return $result['maxId'];
        }
        return false;
    }
}